package org.alex.samples.dao;

import java.util.ArrayList;
import java.util.List;

import org.alex.samples.model.ExamTest;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class TestDAOImp extends BaseHibernateDAO<ExamTest> implements ITestDAO{
	
	@Override
	public List<ExamTest> findAllTest() {
		List<ExamTest> list =  this.find("FROM ExamTest");
		return list;
	}
	

	@Override
	public void createTest(ExamTest test) throws Exception {
		this.save(test);
	}

	@Override
	public ExamTest getTestById(Integer id) {
		ExamTest exsitsTest = this.get(id);
		if (exsitsTest != null) {
			return exsitsTest;
		}else{
			return null;
		}
	}

	@Override
	public void editTest(ExamTest test) throws Exception {
		this.update(test);
	}

	@Override
	public void deleteTest(Integer id) throws Exception {
		ExamTest exsitsTest = this.get(id);
		if (exsitsTest != null) {
			this.delete(exsitsTest);
		}
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<ExamTest> getTestListBySubAndTestType(Integer subId, Integer testTypeId) {
		String hql = " FROM ExamTest es WHERE es.subId = :subId AND es.examTestType.id = :testTypeId ";
		return this.createQuery(hql)
				.setInteger("subId", subId)
				.setInteger("testTypeId", testTypeId)
				.list();
	}


	@Override
	public List<ExamTest> getTestListByIdList(List<Integer> randomTestIdList) {
		List<ExamTest> TestList = new ArrayList<ExamTest>();
		
		for(Integer id : randomTestIdList){
			ExamTest exsitsTest = this.get(id);
			TestList.add(exsitsTest);
		}
		
		return TestList;
	}
	
	
	
/*
	@Autowired
	private SessionFactory sessionFactroy;
	
	public Session getSession(){
		return this.sessionFactroy.getCurrentSession();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<ExamTest> findAllTest() {
		Session session = getSession();
		String hql = "FROM ExamTest";
		List<ExamTest> examTest = session.createQuery(hql).list();
		return examTest;
	}

	@Override
	public void createTest(ExamTest test) throws Exception {
		Session session = getSession();
		session.save(test);
	}

	@Override
	public ExamTest getTestById(Integer id) {
		Session session = getSession();
		return (ExamTest) session.get(ExamTest.class, id);
	}

	@Override
	public void editTest(ExamTest test) throws Exception {
		Session session = getSession();
		session.update(test);
	}

	@Override
	public void deleteTest(Integer id) throws Exception {
		Session session = getSession();
		ExamTest examTest = (ExamTest) session.get(ExamTest.class, id);
		if(examTest != null){
			session.delete(examTest);
		}
	}
*/
}
